<?php
require_once 'global.func.php';
class Pagination
{
	private $_perPage;
	private $_currentPage;
	private $_totalPage;
	private $_params;
	
	public function __construct($itemCount, $perPage=15)
	{
		$this->_perPage = $perPage;
		$this->_totalPage = ceil($itemCount / $this->_perPage);
		if( $this->_totalPage>100 ) $this->_totalPage = 100;
		$this->_currentPage = isset($_GET['page']) ? intval(mysql_escape_string($_GET['page'])) : 1;
		$this->initGetParams();
	}
	
	private function initGetParams()
	{
		unset($_GET['page']);
		foreach ($_GET as $k => $v) {
			$this->_params .= $k.'='.$v . '&';
		}
	}
	
	public function getPagination()
	{
		$outPut = '';
		$lastPage = $this->_totalPage;
		$targetPage = $_SERVER['PHP_SELF'];
		$stages = 3;
		$LastPagem1 = $lastPage - 1;
		
		if($this->_currentPage <=0)$this->_currentPage == 1;
		$prev = $this->_currentPage - 1;
		$next = $this->_currentPage + 1;
		if($lastPage > 1)
		{
			$outPut .= '<div class="paginate">';
			if ($this->_currentPage > 1){
				$prev = $this->_currentPage - 1;
				$outPut.= "<a href='$targetPage?page=$prev&$this->_params'>上一页</a>";
			}else{
				$outPut.= "<span class='disabled'>下一页</span>";	}
		}
			

		// Pages	
		if ($lastPage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastPage; $counter++)
			{
				if ($counter == $this->_currentPage){
					$outPut.= "<span class='current'>$counter</span>";
				}else{
					$outPut.= "<a href='$targetPage?page=$counter&$this->_params'>$counter</a>";}					
			}
		}
		elseif($lastPage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($this->_currentPage < 1 + ($stages * 2))		
			{
				for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$outPut.= "<a href='$targetPage?page=$counter&$this->_params'>$counter</a>";}					
				}
				$outPut.= "...";
				$outPut.= "<a href='$targetPage?page=$LastPagem1&$this->_params'>$LastPagem1</a>";
				$outPut.= "<a href='$targetPage?page=$lastPage&$this->_params'>$lastPage</a>";
			}
			// Middle hide some front and some back
			elseif($lastPage - ($stages * 2) > $this->_currentPage && $this->_currentPage > ($stages * 2))
			{
				$outPut.= "<a href='$targetPage?page=1&$this->_params'>1</a>";
				$outPut.= "<a href='$targetPage?page=2&$this->_params'>2</a>";
				$outPut.= "...";
				for ($counter = $this->_currentPage - $stages; $counter <= $this->_currentPage + $stages; $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$outPut.= "<a href='$targetPage?page=$counter&$this->_params'>$counter</a>";}					
				}
				$outPut.= "...";
				$outPut.= "<a href='$targetPage?page=$LastPagem1&$this->_params'>$LastPagem1</a>";
				$outPut.= "<a href='$targetPage?page=$lastPage&$this->_params'>$lastPage</a>";		
			}
			// End only hide early pages
			else
			{
				$outPut.= "<a href='$targetPage?page=1&$this->_params'>1</a>";
				$outPut.= "<a href='$targetPage?page=2&$this->_params'>2</a>";
				$outPut.= "...";
				for ($counter = $lastPage - (2 + ($stages * 2)); $counter <= $lastPage; $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$outPut.= "<a href='$targetPage?page=$counter&$this->_params'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($this->_currentPage < $counter - 1){ 
			$outPut.= "<a href='$targetPage?page=$next&$this->_params'>下一页</a>";
		}else{
			$outPut.= "<span class='disabled'>下一页</span>";
		}
			
		$outPut.= "</div>";
		return $outPut;		
	}
	
	public function ajaxPagination($currentPage)
	{
		$outPut = '';
		$lastPage = $this->_totalPage;
		$targetPage = $_SERVER['PHP_SELF'];
		$stages = 3;
		$LastPagem1 = $lastPage - 1;
		$this->_currentPage = $currentPage;
		if($this->_currentPage <=0)$this->_currentPage == 1;
		$prev = $this->_currentPage - 1;
		$next = $this->_currentPage + 1;
		if( $this->_totalPage == 0 )return '';
		if($lastPage > 1)
		{
			$outPut .= '<div class="paginate">';
			if ($this->_currentPage > 1){
				$prev = $this->_currentPage - 1;
				$outPut.= "<a href='#' onclick='comment($prev);return false;'>上一页</a>";
			}else{
				$outPut.= "<span class='disabled'>下一页</span>";	}
		}
		
		if($lastPage == 1)
		{
			$outPut .= '<div class="paginate">';
				$outPut.= "<span class='disabled'>上一页</span>";
		}
			

		// Pages	
		if ($lastPage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastPage; $counter++)
			{
				if ($counter == $this->_currentPage){
					$outPut.= "<span class='current'>$counter</span>";
				}else{
					$outPut.= "<a href='#' onclick='comment($counter);return false;'>$counter</a>";}					
			}
		}
		elseif($lastPage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($this->_currentPage < 1 + ($stages * 2))		
			{
				for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$outPut.= "<a onclick='comment($counter);'>$counter</a>";}					
				}
				$outPut.= "...";
				$outPut.= "<a href='#'  onclick='comment($LastPagem1);return false;'>$LastPagem1</a>";
				$outPut.= "<a href='#' onclick='comment($lastPage);return false;'>$lastPage</a>";
			}
			// Middle hide some front and some back
			elseif($lastPage - ($stages * 2) > $this->_currentPage && $this->_currentPage > ($stages * 2))
			{
				$outPut.= "<a href='#' onclick='comment(1);return false;'>1</a>";
				$outPut.= "<a href='#' onclick='comment(2);return false;'>2</a>";
				$outPut.= "...";
				for ($counter = $this->_currentPage - $stages; $counter <= $this->_currentPage + $stages; $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$outPut.= "<a href='#' onclick='comment($counter);return false;'>$counter</a>";}					
				}
				$outPut.= "...";
				$outPut.= "<a href='#' onclick='comment($LastPagem1);return false;'>$LastPagem1</a>";
				$outPut.= "<a href='#' onclick='comment($lastPage);return false;'>$lastPage</a>";		
			}
			// End only hide early pages
			else
			{
				$outPut.= "<a href='#' onclick='comment(1);return false;'>1</a>";
				$outPut.= "<a href='#' onclick='comment(2);return false;'>2</a>";
				$outPut.= "...";
				for ($counter = $lastPage - (2 + ($stages * 2)); $counter <= $lastPage; $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$outPut.= "<a href='#' onclick='comment($counter);return false;'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($this->_currentPage < $counter - 1){ 
			$outPut.= "<a href='#'  onclick='comment($next);return false;'>下一页</a>";
		}else{
			$outPut.= "<span class='disabled'>下一页</span>";
		}
			
		$outPut.= "</div>";
		return $outPut;	
	}
	
	public function getPaginationRewrite()
	{
		$params = array(
			'catid'=>intval($_GET['catid']), 
			'page'=>$this->_currentPage, 
			'sort'=>isset($_GET['sort'])?$_GET['sort']:0 
		);
		$outPut = '';
		$lastPage = $this->_totalPage;
		$targetPage = substr($_SERVER['PHP_SELF'], 0, -4);
		$stages = 3;
		$LastPagem1 = $lastPage - 1;
		
		if($this->_currentPage <=0)$this->_currentPage == 1;
		$prev = $this->_currentPage - 1;
		$next = $this->_currentPage + 1;
		if($lastPage > 1)
		{
			$outPut .= '<div class="paginate">';
			if ($this->_currentPage > 1){
				$prev = $this->_currentPage - 1;
				//$outPut.= "<a href='$targetPage?page=$prev&$this->_params'>上一页</a>";
				$params['page'] = $prev;
				$outPut.= '<a href='.url($targetPage,$params).'>上一页</a>';
			}else{
				$outPut.= "<span class='disabled'>下一页</span>";	
			}
		}
			

		// Pages	
		if ($lastPage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastPage; $counter++)
			{
				if ($counter == $this->_currentPage){
					$outPut.= "<span class='current'>$counter</span>";
				}else{
					$params['page'] = $counter;
					//$outPut.= "<a href='$targetPage?page=$counter&$this->_params'>$counter</a>";	
					$outPut.= '<a href='.url($targetPage,$params).'>'.$counter.'</a>';				
				}
			}
		}
		elseif($lastPage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($this->_currentPage < 1 + ($stages * 2))		
			{
				for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$params['page'] = $counter;
						$outPut.= '<a href='.url($targetPage,$params).'>'.$counter.'</a>';
					}					
				}
				$outPut.= "...";
				$params['page'] = $LastPagem1;
				$outPut.= '<a href='.url($targetPage,$params).'>'.$LastPagem1.'</a>';
				$params['page'] = $lastPage;
				$outPut.= '<a href='.url($targetPage,$params).'>'.$lastPage.'</a>';
			}
			// Middle hide some front and some back
			elseif($lastPage - ($stages * 2) > $this->_currentPage && $this->_currentPage > ($stages * 2))
			{
				$params['page'] = 1;
				$outPut.= '<a href='.url($targetPage,$params).'>1</a>';
				$params['page'] = 2;
				$outPut.= '<a href='.url($targetPage,$params).'>2</a>';
				$outPut.= "...";
				for ($counter = $this->_currentPage - $stages; $counter <= $this->_currentPage + $stages; $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$params['page'] = $counter;
						$outPut.= '<a href='.url($targetPage,$params).'>'.$counter.'</a>';
					}					
				}
				$outPut.= "...";
				$params['page'] = $LastPagem1;
				$outPut.= '<a href='.url($targetPage,$params).'>'.$LastPagem1.'</a>';
				$params['page'] = $lastPage;
				$outPut.= '<a href='.url($targetPage,$params).'>'.$lastPage.'</a>';		
			}
			// End only hide early pages
			else
			{
				$outPut.= "<a href='$targetPage?page=1&$this->_params'>1</a>";
				$outPut.= "<a href='$targetPage?page=2&$this->_params'>2</a>";
				$outPut.= "...";
				for ($counter = $lastPage - (2 + ($stages * 2)); $counter <= $lastPage; $counter++)
				{
					if ($counter == $this->_currentPage){
						$outPut.= "<span class='current'>$counter</span>";
					}else{
						$params['page'] = $counter;
						$outPut.= '<a href='.url($targetPage,$params).'>'.$counter.'</a>';
					}					
				}
			}
		}
					
				// Next
		if ($this->_currentPage < $counter - 1){ 
			$params['page'] = $next;
			$outPut.= '<a href='.url($targetPage,$params).'>下一页</a>';
		}else{
			$outPut.= "<span class='disabled'>下一页</span>";
		}
			
		$outPut.= "</div>";
		return $outPut;			
	}
}